function tst = bciTestOneVsOneSVM(bci,T)


validationMethod = 'max';

nPairs = length(bci.multiSVM.b0);

tst.distW = zeros(size(T,1),length(bci.multiSVM.classes),length(bci.multiSVM.classes));
tst.distW2 = zeros(size(T,1),nPairs);
for k=1:nPairs;
    if isfield(bci,'cspOvsOmask') && bci.param.CSPfilter>0,
        curT=T(:,bci.cspOvsOmask==k);
    else
        curT=T;
    end
    % get distance to hyperplane; if distW>0, first class predicted else
    % 2nd class of classPair predicted
    tst.distW(:,bci.multiSVM.classPair(k,1),bci.multiSVM.classPair(k,2)) = getWDist(curT',bci.multiSVM.W{k},bci.multiSVM.b0(k));
    tst.distW2(:,k) = tst.distW(:,bci.multiSVM.classPair(k,1),bci.multiSVM.classPair(k,2));
end

score = zeros(size(T,1),length(bci.multiSVM.classes));

if strcmpi(validationMethod,'max'), % most winner
    for c_i=1:length(bci.multiSVM.classes),
        score(:,c_i) = sum(tst.distW(:,c_i,:)>0,3)+sum(tst.distW(:,:,c_i)<0,2);
    end
    [sortedScore sortIdx]=sort(score,2,'descend');
    % If there is some ties, we smooth the output of each classifier
    % by a hyperbolic tangeant and compute the real score for each classes 
    temp = find(sortedScore(:,1)==sortedScore(:,2));
    for k=1:length(temp),
        for c_i=1:length(bci.multiSVM.classes),
            score(temp,c_i) = sum(tanh(tst.distW(temp,c_i,:)),3)+sum(tanh(tst.distW(temp,:,c_i)),2);
        end
        [sortedScore(temp,:) sortIdx(temp,:)]=sort(score(temp,:),2,'descend');
    end
    tst.prediction = bci.multiSVM.classes(sortIdx(:,1));
elseif strcmpi(validationMethod,'sum'), % normalized sum of distW
    for c_i=1:length(bci.multiSVM.classes),
        ref = sum(abs(tst.distW(:,c_i,:)),3)+sum(abs(tst.distW(:,:,c_i)),2);
        normDistW = tst.distW./repmat(ref,[1,length(bci.multiSVM.classes),length(bci.multiSVM.classes)]);
        score(:,c_i) = sum(normDistW(:,c_i,:),3)-sum(normDistW(:,:,c_i),2);
    end
    [dummy maxIdx]=max(score,[],2);
    tst.prediction = bci.multiSVM.classes(maxIdx);
elseif strcmpi(validationMethod,'sumsquare'), % vectornormalized sum of distW
    for c_i=1:length(bci.multiSVM.classes),
        ref = sqrt(sum(tst.distW(:,c_i,:).^2,3)+sum(tst.distW(:,:,c_i).^2,2));
        normDistW = tst.distW./repmat(ref,[1,length(bci.multiSVM.classes),length(bci.multiSVM.classes)]);
%         score(:,c_i) = sum(normDistW(:,c_i,:).^2.*sign(normDistW(:,c_i,:)),3) - ...
%                         sum(normDistW(:,:,c_i).^2.*sign(normDistW(:,:,c_i)),2);
        score(:,c_i) = sum(normDistW(:,c_i,:),3)-sum(normDistW(:,:,c_i),2);
    end
    [dummy maxIdx]=max(score,[],2);
    tst.prediction = bci.multiSVM.classes(maxIdx);
end
tst.score=score;

function distW=getWDist(dat,W,b0)
% get distance to hyperplane

x_dot_w = dat' * (W'*ones(1,size(dat,2)));
x_dot_w = (x_dot_w .* eye(size(dat,2))) * ones(size(dat,2),1);
distW = (x_dot_w + b0) / norm(W);

